function performHistoneClustering_Fig4CD()
% Creates a cluster tree and heatmap of histone modification data (GM06990, chrm 7)
%
% performClustering() creates the cluster tree and heatmap of the histone
% modification data saved in 'GM06990chips_chrm7_RegionsView.mat'. This
% mode of visualization enables to identify group of genes with similar
% histone modification patterns accross the chr. 7 in GM06990, a cancer
% cell line.
% The dataset for clustering was built by integrating data from 3 different
% DAS sources: UCSC Encode, Havana Genes and Genetic Association Database.
%
% SYNTAX: [] = performHistoneClustering()
%
% See also assembleHistoneDSforClustering(), assembleHistoneDS()
%  
%    DASMiner: DAS library and browser for Matlab.
%    Diogo Veiga, Jan 2009.

load GM06990chips_chrm7_RegionsView.mat regions;

data = [];
labelIdx = [];

%Clustering only regions that have measurement in the following chips
%Largest set of regions with histone modifications mesasured is the
%following:
chips = {'GM06990H3K4me1';'GM06990H3K4me2'; 'GM06990H3K4me3';'GM06990H3ac';...
          'GM06990H4ac'};
     
for i=1:size(regions,2)
   
    if (strcmp(char(regions(i).samples),char(chips)))
        labelIdx = [labelIdx i];
        data = [data regions(i).values];
    end
end
columnLabels = {regions.label}';
columnLabels = columnLabels(labelIdx);

for i=1:size(columnLabels)
    
    if (~isempty(regions(labelIdx(i)).geneSymbol))
        columnLabels{i} = [columnLabels{i} ' ' regions(labelIdx(i)).geneSymbol];
    end
    
    if (~isempty(regions(labelIdx(i)).cancerTypes))
        columnLabels{i} = [columnLabels{i} ' ' regions(labelIdx(i)).cancerTypes];
    end
end

rowLabels = cellstr(regions(1).samples);
% cg_s = clustergram(data, 'RowLabels', rowLabels,...
%                                'ColumnLabels', columnLabels,...
%                                'RowPdist', 'euclidean', ...
%                                'Cluster', 2, ...
%                                'ColumnPdist','euclidean', ...
%                                'Impute', @knnimpute);

cg_s = clustergram(data, 'RowLabels', rowLabels,...
                               'ColumnLabels', columnLabels,...
                               'Dimension', 2, ...
                               'Pdist','euclidean');
try
 set(cg_s,'Colormap',redbluecmap);
catch
    
end

% cm = struct('GroupNumber',{1191,1185,598},'Annotation',{'A','B','C'},...
%      'Color',{'b','m','y'});
%  set(cg_s,'ColumnMarker',cm)
 

%Use "export group to workspace" to create a new dendogram for the group
%and then run view(groupXX)